import Queue from './queue';
export default {
	data() {
		return {
			datas: [],
			pageSizes: [15, 30, 45, 60],
			param: {
				pageIndex: 0,
				pageCount: 1,
				recordCount: 0,
				pageSize: 15
			},
			initialize: false,
			loading: true,
			append: true,
		}
	},
	mixins: [Queue],
	computed: {
		hasMore: function() {
			return this.param.pageCount > this.param.pageIndex;
		},
		noData: function() {
			return !(this.param.pageCount > this.param.pageIndex) &&
				this.datas.length === 0;
		},
	},
	methods: {
		index: function(i) {
			return (this.param.pageIndex - 1) * this.param.pageSize + i + 1
		},
		reset: function() {
			this.datas = [];
			this.param = {
				pageIndex: 0,
				pageCount: 1,
				recordCount: 0,
				pageSize: 15
			}
			this.initialize = false;
		},
		doQuery: function() {
			console.log("this.datas: ",this.datas);
			this.page(1);
		},
		changePage: function(index) {
			this.page(index)
		},
		nextPage: function() {
			this.page(++this.param.pageIndex);
		},
		changeSize: function(size) {
			this.param.pageSize = size;
			this.refreshPage();
		},
		refreshPage: function() {
			this.page(this.param.pageIndex)
		},
		sortChange: function(sort) {
			if (sort.prop) {
				this.param.sortField = sort.prop;
				this.param.sortType = sort.order === 'ascending' ? 'ASC' : 'DESC';
				this.doQuery();
			}
		},
		page: function(index) {
			this.param.pageIndex = index || 1;
			this.loading = true;
			this.__task_add(() => {
				return this.__loadPage(this.param);
			});
		},
		__loadPage: async function(param) {
			let page = await this.loadPage(param);
			this.datas = this.append ? this.datas.concat(page.data) : page.data;
			this.param = page.param;
			this.initialize = true;
			this.loading = false;
			return true;
		},
		loadPage: async function() {
			return {};
		}
	}
}